package com.second.controller.order;

import com.second.model.dto.order.ForderDto;
import com.second.model.pojo.FOrd;
import com.second.service.IFOrdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("order")
public class orderController {

    @Autowired
    private IFOrdService ordService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping()
    public String myOrder() {
        return "jsp/order/order";
    }

    @PostMapping("/SelectAllOrd/{userId}")
    @ResponseBody
    public List<ForderDto> SelectAllOrd(@PathVariable("userId") Integer userId) {
        return ordService.SelectAllOrd(userId);
    }

    @PostMapping("/UsedOrd/{userId}")
    @ResponseBody
    public List<ForderDto> UsedOrd(@PathVariable("userId") Integer userId) {
        return ordService.UsedOrd(userId);
    }

    @PostMapping("/outdateOrd/{userId}")
    @ResponseBody
    public List<ForderDto> outdateOrd(@PathVariable("userId") Integer userId) {
        return ordService.outdateOrd(userId);
    }

    @GetMapping("/CencelOrd/{Id}")
    @ResponseBody
    public void CencelOrd(@PathVariable("Id") Integer Id,
                          HttpServletResponse response) throws IOException {
        ordService.CencelOrd(Id);
        response.sendRedirect("http://localhost:8081/fr/order");
    }

    @PostMapping("blind/{ordCode}/{couponId}")
    @ResponseBody
    public void blindOrdAndCoupon(@PathVariable("ordCode") String ordCode,
                                  @PathVariable("couponId") Integer couponId){
        redisTemplate.opsForValue().set(ordCode,couponId.toString(),1, TimeUnit.HOURS);
    }

}
